<template>
  <div class="register-container">
    <!-- <NavigationBar /> -->
    <div class="register-form">
      <h1>注册</h1>
      <form @submit.prevent="register">
        <div class="form-group">
          <label for="username">用户名</label>
          <input v-model="username" type="text" id="username" placeholder="用户名" required />
        </div>
        <div class="form-group">
          <label for="password">密码</label>
          <input v-model="password" type="password" id="password" placeholder="密码" required />
        </div>
        <div class="form-group">
          <label for="email">邮箱</label>
          <input v-model="email" type="email" id="email" placeholder="邮箱" required />
        </div>
        <!-- <div class="form-group">
          <label for="avatar">头像链接</label>
          <input v-model="avatar" type="text" id="avatar" placeholder="头像链接" />
        </div> -->
        <div class="form-group">
          <label for="phone">电话</label>
          <input v-model="phone" type="text" id="phone" placeholder="电话" />
        </div>
        <div class="form-group">
          <label for="gender">性别</label>
          <select v-model="gender" id="gender">
            <option value="male">男</option>
            <option value="female">女</option>
            <option value="other">其他</option>
          </select>
        </div>
        <div class="form-group">
          <label for="address">地址</label>
          <input v-model="address" type="text" id="address" placeholder="地址" />
        </div>
        <button type="submit" class="register-button">注册</button>
        <p class="login-link">已有账号？<a href="#" @click.prevent="goToLogin">登录</a></p>
      </form>
    </div>
    <Footer />
  </div>
</template>

<script>
import Footer from '@/components/Footer.vue';
import axios from 'axios'; 
// import NavigationBar from '@/components/NavigationBar.vue';

export default {
  name: 'Register',
  components: {
    // NavigationBar,
    Footer
  },
  data() {
    return {
      username: '',
      password: '',
      email: '',
      avatar: '',
      phone: '',
      gender: '',
      address: ''
    };
  },
  methods: {
    async register() {
    try {
      const response = await axios.post('http://localhost:3000/register', {
        username: this.username,
        password: this.password,
        email: this.email,
        phone: this.phone,
        gender: this.gender,
        address: this.address
      });
      
      if (response.data.success) {
        alert('注册成功');
        this.$router.push('/');
      } else {
        alert(response.data.error || '注册失败');
      }
    } catch (error) {
      console.error('注册错误详情:', {
        message: error.message,
        response: error.response?.data,
        stack: error.stack
      });
      
      const errorMsg = error.response?.data?.error 
        || error.response?.data?.details
        || error.message
        || '注册失败，请检查输入信息';
      
      alert(errorMsg);
    }
  },
    goToLogin() {
      this.$router.push('/');
    }
  }
};
</script>

<style scoped>
.register-container {
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  height: 100vh;
  background-color: #f4f4f9;
}

.register-form {
  background-color: #ffffff;
  padding: 30px;
  border-radius: 8px;
  box-shadow: 0 4px 8px rgba(0, 0, 0, 0.1);
  width: 100%;
  max-width: 400px;
}

h1 {
  margin-bottom: 20px;
  font-size: 24px;
  text-align: center;
  color: #333333;
}

.form-group {
  margin-bottom: 15px;
}

.form-group label {
  display: block;
  margin-bottom: 5px;
  font-weight: bold;
  color: #666666;
}

.form-group input,
.form-group select {
  width: 100%;
  padding: 10px;
  border: 1px solid #cccccc;
  border-radius: 4px;
  box-sizing: border-box;
  font-size: 14px;
}

.register-button {
  width: 100%;
  padding: 12px;
  background-color: #007bff;
  color: #ffffff;
  border: none;
  border-radius: 4px;
  font-size: 16px;
  cursor: pointer;
  transition: background-color 0.3s;
}

.register-button:hover {
  background-color: #0056b3;
}

.login-link {
  margin-top: 20px;
  text-align: center;
  font-size: 14px;
  color: #007bff;
}

.login-link a {
  color: #007bff;
  text-decoration: none;
  transition: color 0.3s;
}

.login-link a:hover {
  color: #0056b3;
  text-decoration: underline;
}
</style>